<?php
defined('_XR_EXEC') or die();
class session {
	public static $options = array();
	public static $ip;
	public static $agent;

	/* 初始化自己 */
	public static function &singleton(){
		static $instance;
		if(!isset($instance)){
			$instance=new Session();
		}
		return $instance;
	}
	/**
	 * 启用session
	 * SESSION　GC发生的机率，机率的计算公式为gc_probability/gc_divisor
	 * @param array $options 设置选项
	 */
	public function __construct ($options = array()) {
		//$cfgops = factory::getcfg('options',array(),'session');
	 
		//options
		$options = self::$options = array(
			'sessionname'=>factory::getcfg('sessionname','DefaultSessionID','session'),
			'expiration'=>factory::getcfg('expiration',0,'session'),
			'gc_maxlifetime'=>factory::getcfg('gc_maxlifetime','86400','session'),
			'gc_probability'=>factory::getcfg('gc_probability','1','session'),
			'gc_divisor'=>factory::getcfg('gc_divisor','100000','session'),
			'cookie_domain'=>factory::getcfg('cookie_domain','','session'),
			'cookie_path'=>factory::getcfg('cookie_path','/','session'),
			'cookie_secure'=>factory::getcfg('cookie_secure',false,'session'),
			'cookie_httponly'=>factory::getcfg('cookie_httponly',false,'session')
		); 
	 
		// Validate the session name
		if ( ! preg_match('~^(?=.*[a-z])[a-z0-9_]++$~iD', self::$options['sessionname'])){
			die('invalid_session_name');
		} 
		// Name the session, this will also be the name of the cookie
		session_name(self::$options['sessionname']); 
		// Set the session cookie parameters
		session_set_cookie_params
		(
			self::$options['expiration'],
			self::$options['cookie_path'],
			self::$options['cookie_domain'],
			self::$options['cookie_secure'],
			self::$options['cookie_httponly']
		);
		// if you post it for sessionid, will catch it 
		if (isset($_POST[self::$options['sessionname']])) {
			session_id($_POST[self::$options['sessionname']]);
		}
		// Start the session!
		session_start();
		
	//	header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"'); //为IE6.0安全策略发送修改过的头信息
		ini_set('session.gc_maxlifetime', self::$options['gc_maxlifetime']);
		ini_set('session.gc_probability', self::$options['gc_probability']);
		ini_set('session.gc_divisor', self::$options['gc_divisor']);
		self::$ip = self::ip();
		if (isset($_SERVER['HTTP_USER_AGENT'])) self::$agent = $_SERVER['HTTP_USER_AGENT'];
		if (! isset($_SESSION['system_core'])) {
			$time = time();
			$_SESSION['system_core'] = array(
				'session_id' => session_id(), 
				'counter' => 0, 
				'start' => $time, 
				'last' => $time, 
				'now' => $time, 
				'ip' => self::$ip, 
				'agent' => self::$agent
			);
		}
		else {
			$_SESSION['system_core']['counter'] += 1;
			$_SESSION['system_core']['last'] = $_SESSION['system_core']['now'];
			$_SESSION['system_core']['now'] = time();
		}
	}
	
	public static function get($k='',$defaults=NULL){
		if($k == ''){
			return $_SESSION;
		}else{
			$p = $_SESSION;
			$name = explode('.',$k);
			foreach ($name as $_name){
				if(!isset($p[$_name])){
					return $defaults;
				}
				$p = $p[$_name];
			}
			return $p;
		}
	}
	public static function set($k,$v){
    $p = &$_SESSION;
		$name = explode('.',$k);
		foreach ($name as $_name){
			$p = &$p[$_name];
		}
		return $p = $v;
	}

	/**
	 * 获得客户端IP地址
	 *
	 * @return string
	 */
	public static function ip () {
		static $ip;
		if (! isset($ip)) {
			$c = getenv('HTTP_CLIENT_IP');
			$x = getenv('HTTP_X_FORWARDED_FOR');
			$r = getenv('REMOTE_ADDR');
			$sr = $_SERVER['REMOTE_ADDR'];
			if ($c && strcasecmp($c, 'unknown')) {
				$ip = $c;
			}
			elseif ($x && strcasecmp($x, 'unknown')) {
				$ip = $x;
			}
			elseif ($r && strcasecmp($r, 'unknown')) {
				$ip = $r;
			}
			elseif ($sr && strcasecmp($sr, 'unknown')) {
				$ip = $sr;
			}
			$ip = preg_match("/^([\d]{1,3}\.){3}[\d]{1,3}$/", $ip) ? $ip : 'unknown';
		}
		return $ip;
	}


	/**
	 * 销毁session
	 * 
	 */
	public static function destroy () {
		if (session_id() !== '')
		{
			// Get the session name
			$name = session_name();
			// Destroy the session
			session_destroy();
			// Re-initialize the array
			$_SESSION = array();
			// Delete the session cookie cookie类还没写
			//cookie::delete($name);
		}
	}

}